package com.mms.controller;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.Page;
import com.mms.enumerate.CodeEnum;
import com.mms.pojo.Medicine;
import com.mms.service.IMedicineService;
import com.mms.utils.LayUiPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 药品信息
 * 前端控制器
 * </p>
 *
 * @author 陈林鹏
 * @since 2021-04-14
 */
@Controller
@RequestMapping("/medicine")
public class MedicineController {

    @Autowired
    IMedicineService medicineService;


    /**
     * 批量删除药品
     *
     * @param id 药瓶Id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteMedicine")
    public String deleteMedicine(@RequestParam("id[]") List<Integer> id) {
        //批量删除药瓶
        medicineService.deleteBatchIds(id);
        return CodeEnum.failMsg(0, "批量删除成功");
    }

    /**
     * 创建药品界面
     *
     * @return
     */
    @RequestMapping(value = "createJsp")
    public ModelAndView createJsp() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("medicine/createJsp");
        return mv;
    }

    /**
     * 创建药品
     *
     * @param medicine 药品对象
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "createMedicine")
    public String createMedicine(Medicine medicine) {
        medicineService.insert(medicine);
        return CodeEnum.failMsg(0, "创建成功");
    }

    /**
     * 购入入库药瓶
     *
     * @param id        药品id
     * @param inventory 入库的数量 必须 大于 0 的整数
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "warehousing")
    public String warehousing(Integer id, Integer inventory) {
        //设置药瓶Id
        Medicine medicine = medicineService.selectById(id);
        if (inventory < 1) {
            return CodeEnum.failMsg("数量不能小于 1");
        }
        //设置药瓶要 入库的数量
        medicine.setInventory(medicine.getInventory() + inventory);
        medicineService.updateById(medicine);
        return CodeEnum.failMsg(0, "入库成功");
    }

    /**
     * 更新药瓶信息
     *
     * @param medicine 药瓶信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateMedicine")
    public String updateMedicine(Medicine medicine) {
        medicineService.updateById(medicine);
        return CodeEnum.ModifiedSuccessfully.toString();
    }

    /**
     * 获取所有的药品信息
     *
     * @param pageNum 页
     * @param limit   每页的数量
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getAllMedicine")
    public String getAllMedicine(@RequestParam(value = "page") Integer pageNum,
                                 Integer limit) {
        Page page = LayUiPage.parse(pageNum, limit);
        JSONObject retJson = new JSONObject();

        List<Map<String, Object>> retData = medicineService.selectMaps(new EntityWrapper());


        System.out.println(retData);
        if (retData == null) {
            return CodeEnum.AcquisitionFailed.toString();
        }

        retJson.put("code", 0);
        retJson.put("msg", "获取成功");
        retJson.put("data", retData);
        retJson.put("count", page.getTotal());
        page.close();
        return retJson.toString(SerializerFeature.DisableCircularReferenceDetect);
    }
}

